<?php
namespace app\hhc\controller;

use app\common\controller\Frontend;
use think\Controller;
use think\Request;
use think\Db;
use fast\lucn\Lucn;
/**
 * about控制器
 * @author  Lucn30 <Lucn30@126.com>
 */

class Index extends Base{
    public function _initialize(){
		parent::_initialize();
		    define('TABLE', 'hhc');//定义数据表名。
			define('SIGN', 'index');//定义标识
			//define('PID', 1);//定义类别
			$this->assign('controller_sign',SIGN);//控制器
			$this->assign('controller_page','黄花菜专题');
			$this->assign('method_sign',SIGN);
	}
    public function index(){
		$where['status'] = 'normal';
		/* SLIDER */
		$where['category_id'] = 20;
		$slider = Db::name('slider')
		  ->where($where)
		  ->field('id,title,link,image,size')
		  ->select();
		foreach($slider as $i => $son){
			$slider[$i]['size'] = explode('x',$son['size']);
		}
		$this->assign('slider',$slider);
		unset($where);
		
		/* 头条 */
		$where['flag'] = array('EQ','index');
		$where['status'] = 'normal';
		$news = Db::name(TABLE)
		  ->where($where)
		  ->field('id,title,category_id,images,description')
		  ->order('weigh desc')
		  ->limit(6)
		  ->select();
		$this->assign('news',$news);
		unset($where);
		
		/* 分类文章 */
		//文化
		$where['category_id'] = 14;
		$where['status'] = 'normal';
		$where['flag'] = array('EQ','index');
		$list = array();
		$list[0][0] = Db::name(TABLE)
		  ->where($where)
		  ->field('id,title,category_id,image')
		  ->order('weigh desc')
		  ->limit(1)
		  ->find();
		unset($where['flag']);
		$where['id'] = array('NEQ',$list[0][0]['id']);
		$list[0][1] = Db::name(TABLE)
		  ->where($where)
		  ->field('id,title,category_id')
		  ->order('createtime desc')
		  ->limit(6)
		  ->select();
		//养生
		$where['category_id'] = 16;
		$where['flag'] = array('EQ','index');
		$list[1][0] = Db::name(TABLE)
		  ->where($where)
		  ->field('id,title,category_id,image')
		  ->order('weigh desc')
		  ->limit(1)
		  ->find();
		unset($where['flag']);
		$where['id'] = array('NEQ',$list[1][0]['id']);
		$list[1][1] = Db::name(TABLE)
		  ->where($where)
		  ->field('id,title,category_id')
		  ->order('createtime desc')
		  ->limit(6)
		  ->select();
		//常识
		$where['category_id'] = 18;
		$where['flag'] = array('EQ','index');
		$list[2][0] = Db::name(TABLE)
		  ->where($where)
		  ->field('id,title,category_id,image')
		  ->order('weigh desc')
		  ->limit(1)
		  ->find();
		unset($where['flag']);
		$where['id'] = array('NEQ',$list[2][0]['id']);
		$list[2][1] = Db::name(TABLE)
		  ->where($where)
		  ->field('id,title,category_id')
		  ->order('createtime desc')
		  ->limit(6)
		  ->select();
		$this->assign('list',$list);
		unset($where);
		
		/* 食谱 */
		$where['category_id'] = 15;
		$where['status'] = 'normal';
		$where['flag'] = array('EQ','index');
		$caipu[0] = Db::name(TABLE)
		  ->where($where)
		  ->field('id,title,category_id,image')
		  ->order('weigh desc')
		  ->limit(1)
		  ->find();
		unset($where['flag']);
		$where['id'] = array('NEQ',$caipu[0]['id']);
		$caipu[1] = Db::name(TABLE)
		  ->where($where)
		  ->field('id,title,category_id,image')
		  ->order('createtime desc')
		  ->limit(6)
		  ->select();
		$this->assign('caipu',$caipu);
		//dump($caipu);die;
		
		/* 行情 */
		$where['category_id'] = 17;
		$where['status'] = 'normal';
		$hangqing = Db::name(TABLE)
		  ->where($where)
		  ->field('id,title,category_id,createtime')
		  ->order('createtime desc')
		  ->limit(12)
		  ->select();
		$this->assign('hangqing',$hangqing);
		//dump($caipu);die;
		
		/* 技术 */
		$where['category_id'] = 19;
		$where['status'] = 'normal';
		$jishu = Db::name(TABLE)
		  ->where($where)
		  ->field('id,title,category_id,createtime')
		  ->order('createtime desc')
		  ->limit(12)
		  ->select();
		$this->assign('jishu',$jishu);
		unset($where);
		
		/* 图赏 */
		$where['status'] = 'normal';
		$where['flag'] = array('EQ','recommend');
		$tushang = Db::name(TABLE)
		  ->where($where)
		  ->field('id,title,category_id,createtime,image')
		  ->order('createtime desc')
		  ->limit(10)
		  ->select();
		$this->assign('tushang',$tushang);
		//dump($caipu);die;
		
		$this->assign('title',$this->sitename);
		$this->assign('keyword','黄花菜介绍,黄花菜文化,金针菇,黄花菜怎么做,黄花菜的营养,淮阳黄花菜,祁东黄花菜,邵东黄花菜,中国黄花菜大全,黄花菜百科知识');
		$this->assign('description','中国黄花菜为您提供黄花菜文化、菜谱、养生、行情、新闻、技术、供求相关信息，是中国专业的黄花菜专题网站。');
		return $this->Fetch('/index');
    }
	
	public function search(){
		$keyword = input('keyword');
		$map['status'] = 'normal';
		$map['keywords'] = array('LIKE','%'.$keyword.'%');
		$list = Db::name(TABLE)
		->where($map)
        ->field('id,title,description,createtime,image,images,author,views')
		->order('createtime desc')
        ->paginate(10);
		
		//热点
		$map4['status'] = 'normal';
		$hlist = Db::name(TABLE)
        ->where($map4)
		->field('id,title,image,views')
		->order('views desc,createtime desc')
		->limit(6)
        ->select();
		
		$site_title = $keyword.'_'.$this->sitename.'搜索结果';
		$this->assign('hlist', $hlist);
		$this->assign('title', $site_title);
		$this->assign('list_category',$this->category);
		$this->assign('list',$list);
		$this->assign('keyword',$keyword.'_'.$this->sitename);
		$this->assign('description','有关'.$keyword.'的搜索结果');
		return $this->Fetch('/list');
	}
	
	public function tolist(){
	      $cid = input('cid/d',0);
		  if($cid != 0){$map['category_id'] = $cid;}
		  //$map2['type'] = SIGN;
		  
		  foreach($this->category as $i => $son){
		      if($son['id'] == $cid){
			      $one_category = $son;
			  }
		  }
		  
		  if(empty($one_category)){
		      $one_category['id'] = 0;
			  $one_category['name'] = '黄花菜专题';
		  }
		
		//列表
        $map['status'] = 'normal';
		$list = Db::name(TABLE)
        ->where($map)
        /*->distinct(true)*/
        ->field('id,title,description,createtime,image,images,author,views')
		->order('createtime desc')
        ->paginate(10);
		
		//本类热点
		$map4['status'] = 'normal';
		if($one_category['id']) $map4['category_id'] = $one_category['id'];
		$hlist = Db::name(TABLE)
        ->where($map4)
		->field('id,title,image,views')
		->order('views desc,createtime desc')
		->limit(6)
        ->select();
		
		$site_title = $one_category['name'].'_'.$this->sitename;
		$this->assign('hlist', $hlist);
		$this->assign('title', $site_title);
		$this->assign('list_category',$this->category);
		$this->assign('one_category',$one_category);
		$this->assign('list',$list);
		$this->assign('keyword','黄花菜_'.$one_category['name'].'_'.$this->sitename);
		$this->assign('description','黄花菜'.$one_category['name'].'的列表页面');
		return $this->Fetch('/list');
    }
	
	public function view(){
	    $id = input('id/d',0);

		if($id != 0){
		    Db::name(TABLE)
            ->where('id', $id)
            ->setInc('views');
		}
		//$map['status'] = 'normal';
		$map['id'] = $id;
		$one = Db::name(TABLE)
        ->where($map)
        ->find();
		if($one){
		$one['createtime2'] = Lucn::countDownTime($one['createtime']);
		foreach($this->category as $i => $son){
		    if($son['id'] == $one['category_id']){
			    $one_category = $son;
			}
		}
		}
		//下面推荐阅读
		$map3['category_id'] = $one['category_id'];
		$map3['id'] = array('NEQ',$one['id']);
		$map3['flag'] = array('EQ','recommend');
		
		$olist = Db::name(TABLE)
        ->where($map3)
		->field('id,title,image,images,author,views')
		->order('createtime desc')
		->limit(6)
        ->select();
		
		//本类热点
		$map4['category_id'] = $one['category_id'];
		$map4['id'] = array('NEQ',$one['id']);
		$hlist = Db::name(TABLE)
        ->where($map4)
		->field('id,title,image,views')
		->order('views desc,createtime desc')
		->limit(6)
        ->select();

		$this->assign('olist',$olist);
		$this->assign('hlist',$hlist);
		$this->assign('title',$one['title'].'_'.$this->sitename);
		$this->assign('list_category',$this->category);
		$this->assign('one_category',$one_category);
		$this->assign('one',$one);
		$this->assign('keyword',$one['keywords']);
		$this->assign('description',$one['description']);
        return $this->Fetch('/view');
    }
}